let { HttpRequest, globalData, mfutil, UserAccess, addUserStat, URLChangeCommon } = getApp();
const app = getApp();

Page({
  data: {
    tilesDetail: {  // 详情数据
      tile_info: '',
      points: '',       //需要积分数值
    },
    scrolly: true,
    scrollTop: 0,
    a: false,
    btn: true, // 抽奖按钮是否可以点击
    userActive: null,   //用户是否可以参与活动
    radioActive: false,
    router: 'pages/draw-prize/draw-prize',
    isTurnOver: true
  },
  onLoad(options) {
    my.hideShareMenu({
      success() {
      },
      fail() {
      },
    });
    console.log(options, "opentions---------------------------------------------");
    let { id } = options;
    this.setData({
      id,
    })
    console.log(options, "qqqqqqqqqqqqqqqqqqqqqqqqqeeeeewwwwwww1222");
    this.initPage()
    /**
* 用户行为记录埋点
*/
    let routerArr = getCurrentPages();
    let params = app.globalData.params;
    let channel = app.globalData.channel;
    let UserAccessoptions = {
      grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
      name: routerArr[routerArr.length - 1].data.pagetitle,
      address: options.id,
    }
    let track = UserAccess(UserAccessoptions)
    // 自定义埋点
    let customAccessoptions = {
      customType: 'TileCover_' + options.id,
      // tileId: options.id
    }
    console.log(options.id, '进入奖品页面');
    let tracks = addUserStat(customAccessoptions)
  },
  // 初始化数据
  async initPage() {
    let { tilesDetail, id, userActive } = this.data;
    let path = `/vans-family/api/v2.1/project/vansFamily/cms/forward/get?url=/v2/cms/api/tile/${id}`;
    let parmasObj = {
      member_id_type: 1,
      member_id: globalData.userInfo.memberId
    }
    this.setData({
      loadingActive: true
    })
    let res = await HttpRequest(path, 'POST', parmasObj, {})
    console.log(res,"button")
    this.setData({
      list_button_text:res.data.data.tile_info.list_button_text
    })
    //积分查询 
    let bodyObj2 = {
      "param": globalData.ouid,
      "type": 3
    }
    let path2 = `/vans-family/api/v2.1/project/vansFamily/clm/forward/sendjson/get?url=/v1/points/api/tmall/account`;
    let res2 = await HttpRequest(path2, 'POST', '', bodyObj2);
    if (res2.data.resultCode == 0) {
      globalData.userInfo.availablePoint = res2.data.data.availablePoint;
      globalData.userInfo.willExpirePoint = res2.data.data.willExpirePoint;
    }
    console.log(res2.data, "=============+++++++++++++++++++==============+");

    if (res.data.code == 0) {
      tilesDetail = res.data.data;
      // complete_text=res.data.data.tile_info.complete_text
      // console.log(complete_text,"[[[[");
      let { availablePoint } = globalData.userInfo;
      let { points, type } = tilesDetail.tile_info;
      userActive = (availablePoint >= points) || points == 0 ? true : false    //用户可用积分/活动所需积分
      this.setData({
        tilesDetail,
        userActive,
        a: true

      })
      this.setData({
        loadingActive: false
      })
      this.creatCanvas()
    }
    this.setData({
      loadingActive: false
    })
    this.setData({
      loadingActive: false
    })

  },

  // 点击抽奖
  async clickPrize() {

    let that = this;
    // 点击抽奖按钮
    if (!that.data.btn) {
      console.log("&&&&&&&")
      return;
    }
    that.setData({ btn: false });

    let { tilesDetail, id } = this.data;
    let time = new Date(+new Date() + 8 * 3600 * 1000).toJSON().substr(0, 19).replace("T", " ").replace(/\-/g, "/")
    let jumpObj = {
      member_id: globalData.userInfo.memberId,
      member_id_type: 1,
    }
    let timeObj = {
      do_datetime: time
    }
    let path = `/vans-family/api/v2.1/project/vansFamily/cms/forward/post?url=/v1/cms/api/tile/${tilesDetail.tile_info.type}/${tilesDetail.tile_info.id}/done`;

    let res = await HttpRequest(path, 'POST', jumpObj, timeObj)

    console.log(res, "res@@@@@@@@@@@@@@______@@@@@@@@@#");
    tilesDetail = res.data.data
    tilesDetail.lucky_draw = res.data.data.lucky_draw
    tilesDetail.lucky_draw.cta = res.data.data.lucky_draw.cta
    console.log(tilesDetail.cta, "ooooooooooooooooooooooooooooooooooooooooo");
    if (res.data.code == 0) {
      if (tilesDetail.lucky_draw.prize_type === 'coupon') { // 优惠卷 tile_page
        let url = `/pages/prize_page/prize_page?id=${id}&lucky_text=${tilesDetail.lucky_draw.lucky_text}&lucky_image=${tilesDetail.lucky_draw.lucky_image}&prize_name=${tilesDetail.lucky_draw.prize_name}&cta=${encodeURIComponent(JSON.stringify(tilesDetail.lucky_draw.cta))}`
        console.log(url);
        my.redirectTo({
          url
        });
      } else if (tilesDetail.lucky_draw.prize_type === 'points') { // 积分 

        my.redirectTo({
          url: `/pages/integrall_page/integrall_page?id=${id}&lucky_text=${tilesDetail.lucky_draw.lucky_text}&lucky_image=${tilesDetail.lucky_draw.lucky_image}&prize_name=${tilesDetail.lucky_draw.prize_name}&cta=${encodeURIComponent(JSON.stringify(tilesDetail.lucky_draw.cta))}`
        });
      } else if (tilesDetail.lucky_draw.prize_type === 'ma') { // MA

        my.redirectTo({
          url: `/pages/ma_page/ma_page?id=${id}&lucky_text=${tilesDetail.lucky_draw.lucky_text}&lucky_image=${tilesDetail.lucky_draw.lucky_image}&prize_name=${tilesDetail.lucky_draw.prize_name}&cta=${encodeURIComponent(JSON.stringify(tilesDetail.lucky_draw.cta))}`
        });
      } else if (tilesDetail.lucky_draw.prize_type === 'tile') { // tile

        my.redirectTo({
          url: `/pages/tile_page/tile_page?id=${id}&lucky_text=${tilesDetail.lucky_draw.lucky_text}&lucky_image=${tilesDetail.lucky_draw.lucky_image}&prize_name=${tilesDetail.lucky_draw.prize_name}&cta=${encodeURIComponent(JSON.stringify(tilesDetail.lucky_draw.cta))}`
        });
      } else if (tilesDetail.lucky_draw.prize_type === 'store') { //门店自提
        my.redirectTo({
          url: `/pages/store_page/store_page?id=${id}&lucky_text=${tilesDetail.lucky_draw.lucky_text}&lucky_image=${tilesDetail.lucky_draw.lucky_image}&prize_name=${tilesDetail.lucky_draw.prize_name}&prize_sku=${tilesDetail.lucky_draw.prize_sku}&cta=${encodeURIComponent(JSON.stringify(tilesDetail.lucky_draw.cta))}&type=${tilesDetail.lucky_draw.prize_type}&done_id=${tilesDetail.done_record_id}`
        });
      } else if (tilesDetail.lucky_draw.prize_type === 'express') { //快递到家
        my.redirectTo({
          url: `/pages/express_page/express_page?id=${id}&lucky_text=${tilesDetail.lucky_draw.lucky_text}&lucky_image=${tilesDetail.lucky_draw.lucky_image}&prize_name=${tilesDetail.lucky_draw.prize_name}&prize_sku=${tilesDetail.lucky_draw.prize_sku}&cta=${encodeURIComponent(JSON.stringify(tilesDetail.lucky_draw.cta))}&type=${tilesDetail.lucky_draw.prize_type}&done_id=${tilesDetail.done_record_id}`
        });
      } else if (tilesDetail.lucky_draw.prize_type === 'digital_pdf') { //电子图片 pdf
        my.redirectTo({
          url: `/pages/electron_prize/electron_prize?id=${id}&lucky_text=${tilesDetail.lucky_draw.lucky_text}&lucky_image=${tilesDetail.lucky_draw.lucky_image}&prize_name=${tilesDetail.lucky_draw.prize_name}&prize_sku=${tilesDetail.lucky_draw.prize_sku}&cta=${encodeURIComponent(JSON.stringify(tilesDetail.lucky_draw.cta))}&urls=${tilesDetail.lucky_draw.prize_one_gift_url}`
        });
      } else if (tilesDetail.lucky_draw.prize_type === 'digital_pic') { //数字礼品 单张图片
        my.redirectTo({
          url: `/pages/emoticon_page/emoticon_page?&id=${id}&lucky_text=${tilesDetail.lucky_draw.lucky_text}&lucky_image=${tilesDetail.lucky_draw.lucky_image}&prize_name=${tilesDetail.lucky_draw.prize_name}&prize_sku=${tilesDetail.lucky_draw.prize_sku}&cta=${encodeURIComponent(JSON.stringify(tilesDetail.lucky_draw.cta))}&urls=${tilesDetail.lucky_draw.prize_one_gift_url}`
        });
      } else if (tilesDetail.lucky_draw.prize_type === 'digital_multi_pic') { //数字礼品多张图片
        my.redirectTo({
          url: `/pages/wallpaper_page/wallpaper_page?id=${id}&lucky_text=${tilesDetail.lucky_draw.lucky_text}&lucky_image=${tilesDetail.lucky_draw.lucky_image}&prize_name=${tilesDetail.lucky_draw.prize_name}&prize_sku=${tilesDetail.lucky_draw.prize_sku}&cta=${encodeURIComponent(JSON.stringify(tilesDetail.lucky_draw.cta))}&urls=${JSON.stringify(tilesDetail.lucky_draw.prize_multi_gift_url)}`
        });
      } else if (tilesDetail.lucky_draw.prize_type === 'no_prize') { //没有奖品
        console.log("11111111111111111111");
        my.redirectTo({
          url: `/pages/no_prize/no_prize?id=${id}&lucky_text=${tilesDetail.lucky_draw.lucky_text}&lucky_image=${tilesDetail.lucky_draw.lucky_image}&prize_name=${tilesDetail.lucky_draw.prize_name}&cta=${encodeURIComponent(JSON.stringify(tilesDetail.lucky_draw.cta))}`
        });
      }
    } else if (res.data.code == 53012 || res.data.code == 53014 || res.data.code == 53001) {
      my.alert({
        content: res.data.msg
      })
    } else {
      mfutil.launchErrPage(53001)
    }
    let timeOut = setTimeout(() => {
      this.setData({ btn: true });
      clearTimeout(timeOut);
    }, 500);
  },
  //单选协议同意/默认
  radioChange() {
    let { radioActive } = this.data;
    this.setData({
      radioActive: !radioActive
    })
  },
  /**
 * 链接文字点击
 */
  hyperlinkChange() {
    let { hyperlinkAcitve } = this.data;
    this.setData({
      hyperlinkAcitve: !hyperlinkAcitve
    })
  },
  //赚更多积分跳转
  getIntegralTo() {
    my.alert({
      content: '您的积分不足，无法参与兑礼！'
    })

  },

  onScroll(event) {
    this.setData({
      scrollTop: event.detail.scrollTop,
      ScrollToUpper: scrollTop == 0 ? true : false
    })
    if (scrollTop == 0) {
      this.setData({
        ScrollToUpper: true
      })
    }
  },
  onTouchStart(event) {
    console.log(event, 'onTouchStartonTouchStart')
    this.setData({
      starty: event.changedTouches[0].pageY,
    })
  },
  onTouchMove(event) {
    //当前pagey
    this.setData({
      movey: event.changedTouches[0].pageY
    })
    let { scrollTop } = this.data;
    let { ScrollToUpper, ScrollToLower, movey, starty } = this.data;
    if (ScrollToUpper && (starty > movey)) {
      this.setData({
        scrolly: true,
        ScrollToUpper: false,
        ScrollToLower: false
      })
    } else
      if (ScrollToUpper && (starty < movey)) {
        this.setData({
          scrolly: false,
        })
      }
    if (ScrollToLower && (starty < movey)) {
      this.setData({
        scrolly: true,
        ScrollToUpper: false,
        ScrollToLower: false
      })
    } else
      if (ScrollToLower && (starty > movey)) {
        this.setData({
          scrolly: false,
        })
      }
  },
  onScrollToUpper(event) {
    this.setData({
      scrolly: false,
      ScrollToUpper: true,
      ScrollToLower: false
    })
  },
  onScrollToLower() {
    this.setData({
      scrolly: false,
      ScrollToLower: true,
      ScrollToUpper: false
    })
  }

})